//
//  RegisterViewController.m
//  豆瓣
//
//  Created by dllo on 15/11/23.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "RegisterViewController.h"
#import <sqlite3.h>
#import "User.h"

@interface RegisterViewController () <UITextFieldDelegate>

@property (nonatomic, retain) UITextField *userField;
@property (nonatomic, retain) UITextField *passwordField;
@property (nonatomic, retain) UITextField *confirmPasswordField;
@property (nonatomic, retain) UITextField *emailField;
@property (nonatomic, retain) UITextField *contactField;

@end

@implementation RegisterViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    
  /********************************** 注册界面 ***************************************/
    
    
    self.navigationItem.title = @"注册";
    
    UIImageView *imageV = [[UIImageView alloc] initWithFrame:self.view.frame];
    imageV.image = [UIImage imageNamed:@"bg-568h@2x.png"];
    [self.view addSubview:imageV];
    [imageV release];

// 用户名
    UILabel *userLabel = [[UILabel alloc] initWithFrame:CGRectMake(30, 50, 100, 50)];
    userLabel.text = @"用户名  :";
    userLabel.font = [UIFont systemFontOfSize:22];
    [self.view addSubview:userLabel];
    [userLabel release];
    
    self.userField = [[UITextField alloc] initWithFrame:CGRectMake(userLabel.frame.origin.x + userLabel.frame.size.width + 20,  userLabel.frame.origin.y + 8, 210, userLabel.frame.size.height - 17)];
    self.userField.placeholder = @"请输入用户名(不少于4位)";
    self.userField.clearButtonMode = UITextFieldViewModeAlways;
    self.userField.borderStyle = UITextBorderStyleRoundedRect;
    self.userField.textAlignment = NSTextAlignmentCenter;
    self.userField.returnKeyType = UIReturnKeyNext;
    self.userField.delegate = self;
    [self.view addSubview:self.userField];
    [self.userField release];

// 密码
    UILabel *passwordLabel = [[UILabel alloc] initWithFrame:CGRectMake(userLabel.frame.origin.x, userLabel.frame.origin.y + userLabel.frame.size.height + 6, userLabel.frame.size.width, userLabel.frame.size.height)];
    passwordLabel.text = @"密    码  :";
    passwordLabel.font = [UIFont systemFontOfSize:22];
    [self.view addSubview:passwordLabel];
    [passwordLabel release];

    self.passwordField = [[UITextField alloc] initWithFrame:CGRectMake(self.userField.frame.origin.x, self.userField.frame.origin.y + self.userField.frame.size.height + 23, self.userField.frame.size.width, self.userField.frame.size.height)];
    self.passwordField.clearButtonMode = UITextFieldViewModeAlways;
    self.passwordField.borderStyle = UITextBorderStyleRoundedRect;
    self.passwordField.textAlignment = NSTextAlignmentCenter;
    self.passwordField.placeholder = @"请输入密码";
    self.passwordField.secureTextEntry = YES;
    self.passwordField.returnKeyType = UIReturnKeyNext;
    self.passwordField.delegate = self;
    [self.view addSubview:self.passwordField];
    [self.passwordField release];
 
// 确认密码
    UILabel *confirmPasswordLabel = [[UILabel alloc] initWithFrame:CGRectMake(userLabel.frame.origin.x, passwordLabel.frame.origin.y + passwordLabel.frame.size.height + 6, userLabel.frame.size.width, userLabel.frame.size.height)];
    confirmPasswordLabel.text = @"确认密码:";
    confirmPasswordLabel.font = [UIFont systemFontOfSize:22];
    [self.view addSubview:confirmPasswordLabel];
    [confirmPasswordLabel release];
    
    self.confirmPasswordField = [[UITextField alloc] initWithFrame:CGRectMake(self.userField.frame.origin.x, self.passwordField.frame.origin.y + self.passwordField.frame.size.height + 23, self.userField.frame.size.width, self.userField.frame.size.height)];
    self.confirmPasswordField.clearButtonMode = UITextFieldViewModeAlways;
    self.confirmPasswordField.borderStyle = UITextBorderStyleRoundedRect;
    self.confirmPasswordField.textAlignment = NSTextAlignmentCenter;
    self.confirmPasswordField.placeholder = @"再次输入密码";
    self.confirmPasswordField.secureTextEntry = YES;
    self.confirmPasswordField.returnKeyType = UIReturnKeyNext;
    self.confirmPasswordField.delegate = self;
    [self.view addSubview:self.confirmPasswordField];
    [self.confirmPasswordField release];

// 邮箱
    UILabel *emailLabel = [[UILabel alloc] initWithFrame:CGRectMake(userLabel.frame.origin.x, confirmPasswordLabel.frame.origin.y + confirmPasswordLabel.frame.size.height + 6, userLabel.frame.size.width, userLabel.frame.size.height)];
    emailLabel.text = @"邮    箱  :";
    emailLabel.font = [UIFont systemFontOfSize:22];
    [self.view addSubview:emailLabel];
    [emailLabel release];
    
    self.emailField = [[UITextField alloc] initWithFrame:CGRectMake(userLabel.frame.origin.x + userLabel.frame.size.width + 20,  emailLabel.frame.origin.y + 8, 210, userLabel.frame.size.height - 17)];
    self.emailField.placeholder = @"请输入用户名";
    self.emailField.clearButtonMode = UITextFieldViewModeAlways;
    self.emailField.borderStyle = UITextBorderStyleRoundedRect;
    self.emailField.textAlignment = NSTextAlignmentCenter;
    self.emailField.returnKeyType = UIReturnKeyNext;
    self.emailField.delegate = self;
    [self.view addSubview:self.emailField];
    [self.emailField release];
  
// 联系人
    UILabel *contactLabel = [[UILabel alloc] initWithFrame:CGRectMake(userLabel.frame.origin.x, emailLabel.frame.origin.y + emailLabel.frame.size.height + 6, userLabel.frame.size.width, userLabel.frame.size.height)];
    contactLabel.text = @"联系人  :";
    contactLabel.font = [UIFont systemFontOfSize:22];
    [self.view addSubview:contactLabel];
    [contactLabel release];
    
    self.contactField = [[UITextField alloc] initWithFrame:CGRectMake(userLabel.frame.origin.x + userLabel.frame.size.width + 20,  contactLabel.frame.origin.y + 8, 210, userLabel.frame.size.height - 17)];
    self.contactField.placeholder = @"请输入联系方式";
    self.contactField.clearButtonMode = UITextFieldViewModeAlways;
    self.contactField.borderStyle = UITextBorderStyleRoundedRect;
    self.contactField.textAlignment = NSTextAlignmentCenter;
    self.contactField.returnKeyType = UIReturnKeyGo;
    self.contactField.delegate = self;
    [self.view addSubview:self.contactField];
    [self.contactField release];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"注册" style:UIBarButtonItemStylePlain target:self action:@selector(rightAction)];
    
    UITapGestureRecognizer *tapGR = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction)];
    [self.view addGestureRecognizer:tapGR];
    [tapGR release];
    
}

// 点击屏幕回收键盘
- (void)tapAction {
    
    [self.userField resignFirstResponder];
    [self.passwordField resignFirstResponder];
    [self.confirmPasswordField resignFirstResponder];
    [self.contactField resignFirstResponder];
    [self.emailField resignFirstResponder];
    
}

// 点击return跳转
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    
    if (textField == self.userField) {
        
        [self.passwordField becomeFirstResponder];
    }
    if (textField == self.passwordField) {
        
        [self.confirmPasswordField becomeFirstResponder];
    }
    if (textField == self.confirmPasswordField) {
        
        [self.emailField becomeFirstResponder];
    }
    if (textField == self.emailField) {
        
        [self.contactField becomeFirstResponder];
    }
    if (textField == self.contactField) {
        
        [self.contactField resignFirstResponder];
        [self rightAction];
    }
    
    return YES;
}

// 保存注册信息
- (void)rightAction {
    
    static sqlite3 *DB = nil;
    
    // 打开数据库
    NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,  NSUserDomainMask, YES) lastObject];
    NSString *filePath = [path stringByAppendingPathComponent:@"dataBase.db"];
    sqlite3_open(filePath.UTF8String, &DB);

    NSLog(@"%@", NSHomeDirectory());
    
    // 用户名不能重复
    // 查找数据库中用户名数组
    NSString *sqlStr = @"SELECT * FROM user";
    sqlite3_stmt *stmt = nil;
    sqlite3_prepare(DB, sqlStr.UTF8String, -1, &stmt, NULL);
    
    while (SQLITE_ROW == sqlite3_step(stmt)) {
        
        const unsigned char *name = sqlite3_column_text(stmt, 1);
        
        NSString *userName = [NSString stringWithUTF8String:(const char *)name];
        
        if ([userName isEqualToString:self.userField.text] == YES) {
            
            UIAlertView *alter = [[UIAlertView alloc] initWithTitle:nil message:@"用户名重复啦" delegate:nil cancelButtonTitle:@"确认" otherButtonTitles:nil];
            NSLog(@"11");
            [alter show];
            [alter release];
            [self.userField resignFirstResponder];
            [self.passwordField resignFirstResponder];
            [self.confirmPasswordField resignFirstResponder];
            [self.contactField resignFirstResponder];
            [self.emailField resignFirstResponder];
            
            return;
        }
    }
    
    // 用户名不能为空
    if (4 > self.userField.text.length) {
        
        UIAlertView *alter = [[UIAlertView alloc] initWithTitle:nil message:@"用户名不能少于4位啊" delegate:nil cancelButtonTitle:@"确认" otherButtonTitles:nil];
        [alter show];
        [alter release];
        [self.userField resignFirstResponder];
        [self.passwordField resignFirstResponder];
        [self.confirmPasswordField resignFirstResponder];
        [self.contactField resignFirstResponder];
        [self.emailField resignFirstResponder];
    }
    
    // 密码不能为空
    else if (0 == self.passwordField.text.length) {
        
        UIAlertView *alter = [[UIAlertView alloc] initWithTitle:nil message:@"密码不能为空啊" delegate:nil cancelButtonTitle:@"确认" otherButtonTitles:nil];
        
        [alter show];
        [alter release];
        [self.userField resignFirstResponder];
        [self.passwordField resignFirstResponder];
        [self.confirmPasswordField resignFirstResponder];
        [self.contactField resignFirstResponder];
        [self.emailField resignFirstResponder];
    }
    // 确认密码不能不一样
    else if (![self.confirmPasswordField.text isEqualToString:self.passwordField.text]) {
        
        UIAlertView *alter = [[UIAlertView alloc] initWithTitle:nil message:@"确认密码错误啦" delegate:nil cancelButtonTitle:@"确认" otherButtonTitles:nil];
        
        [alter show];
        [alter release];
        [self.userField resignFirstResponder];
        [self.passwordField resignFirstResponder];
        [self.confirmPasswordField resignFirstResponder];
        [self.contactField resignFirstResponder];
        [self.emailField resignFirstResponder];
    }
    
    // 向数据库添加
    else {
        
        NSLog(@"%@", NSHomeDirectory());
        
        // 添加用户名, 密码
        User *user = [[User alloc] init];
        
        user.userName = self.userField.text;
        user.userPassword = self.passwordField.text;
        user.userState = @"F";
        
        NSString *sqlStr = [NSString stringWithFormat:@"INSERT INTO user(userName, userPassword, userState) VALUES ('%@', '%@', '%@')", user.userName, user.userPassword, user.userState];
        
        sqlite3_exec(DB, sqlStr.UTF8String, NULL, NULL, NULL);
        
        // 注册成功提示框
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:@"注册成功啦" preferredStyle:UIAlertControllerStyleAlert];
        
        UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
            
            [self.userField resignFirstResponder];
            [self.passwordField resignFirstResponder];
            [self.confirmPasswordField resignFirstResponder];
            [self.contactField resignFirstResponder];
            [self.emailField resignFirstResponder];
           
            [self.navigationController popViewControllerAnimated:YES];
            
        }];
        
        [alert addAction:cancelAction];
        [self presentViewController:alert animated:YES completion:^{
            
            [self.userField resignFirstResponder];
            [self.passwordField resignFirstResponder];
            [self.confirmPasswordField resignFirstResponder];
            [self.contactField resignFirstResponder];
            [self.emailField resignFirstResponder];
            
        }];
        
        // 关闭数据库
        sqlite3_close(DB);
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];

}

@end
